<template>
  <div>
    <el-table :data="items">
      <el-table-column prop="id" label="ID" width="230"> </el-table-column>
      <el-table-column prop="name" label="用户名"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="$router.push(`/custom_users/edit/${scope.row.id}`)"
            >编辑</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="remove(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        layout="prev, pager, next"
        :page-size="size"
        @current-change="current_change"
        :total="total">
      </el-pagination>
  </div>
</template>
<script>
export default {
  data() {
    return {
      items: [],
      total:0,
      size:0
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    async fetch() {
      const res = await this.$http.get('users')
      this.items = res.data.infos.list
      this.total=res.data.infos.total
      this.size=res.data.infos.size
      console.log(this.items + 'mock数据')
    },
    async remove(row) {
      this.$confirm(`此操作将永久删除"${row.name}", 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$http.delete(`user/${row.id}`)
          console.log(res)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetch()
        })
    },
    async current_change(page){
      console.log(page)
      const res =await this.$http.get(`/users?pn=${page}`)
      this.items = res.data.infos.list
    }
  }
}
</script>

